package cn.ch.train.business.service;

import cn.ch.train.business.domain.TrainStation;
import cn.ch.train.business.domain.TrainStationExample;
import cn.ch.train.business.enums.BusinessExceptionEnum;
import cn.ch.train.business.mapper.TrainStationMapper;
import cn.ch.train.business.request.TrainStationQueryParam;
import cn.ch.train.business.request.TrainStationSaveParam;
import cn.ch.train.business.response.TrainStationQueryResult;
import cn.ch.train.common.exception.BusinessException;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class TrainStationService {
    private static final Logger logger = LoggerFactory.getLogger(TrainStationService.class);
    @Resource
    private TrainStationMapper trainStationMapper;

    public void save(TrainStationSaveParam param) {
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(param, TrainStation.class);
        // 保存之前，先校验唯一键是否存在
        TrainStation trainStationDb1 = selectByUnique(param.getTrainCode(), param.getIndex());
        if (Objects.nonNull(trainStationDb1)) {
            throw new BusinessException(BusinessExceptionEnum.TRAIN_STATION_INDEX_HAS_EXIST.getDesc());
        }
        TrainStation trainStationDb2 = selectByUnique(param.getTrainCode(), param.getName());
        if (Objects.nonNull(trainStationDb2)) {
            throw new BusinessException(BusinessExceptionEnum.TRAIN_STATION_NAME_HAS_EXIST.getDesc());
        }
        if (Objects.isNull(trainStation.getId())) {
            trainStation.setId(SnowUtil.getSnowFlakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        } else {
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKey(trainStation);
        }
    }

    private TrainStation selectByUnique(String trainCode, Integer index) {
        TrainStationExample stationExample = new TrainStationExample();
        TrainStationExample.Criteria criteria = stationExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        criteria.andIndexEqualTo(index);
        List<TrainStation> stationList = trainStationMapper.selectByExample(stationExample);
        if (CollUtil.isNotEmpty(stationList)) {
            return stationList.get(0);
        }
        return null;
    }

    private TrainStation selectByUnique(String trainCode, String name) {
        TrainStationExample stationExample = new TrainStationExample();
        TrainStationExample.Criteria criteria = stationExample.createCriteria();
        criteria.andNameEqualTo(name);
        List<TrainStation> stationList = trainStationMapper.selectByExample(stationExample);
        if (CollUtil.isNotEmpty(stationList)) {
            return stationList.get(0);
        }
        return null;
    }

    public PageResult<TrainStationQueryResult> queryList(TrainStationQueryParam param) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("train_code asc");
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(trainStationExample);

        PageInfo<TrainStation> trainStationPageInfo = new PageInfo<>(trainStationList);
        logger.info("总行数：{}", trainStationPageInfo.getTotal());
        logger.info("总页数：{}", trainStationPageInfo.getPages());
        List<TrainStationQueryResult> trainStationQueryList = BeanUtil.copyToList(trainStationList, TrainStationQueryResult.class);
        PageResult<TrainStationQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(trainStationPageInfo.getTotal());
        pageResult.setList(trainStationQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        trainStationMapper.deleteByPrimaryKey(id);
    }
}
